#include "engine.h"
#include "internal_types.h"

namespace HW
{
int CountChildren(TEntity entity)
{
	return ((GameObject::_i_Entity*)(entity))->hierarchy.kids.size();
}

TEntity GetChild(TEntity entity, int index)
{
	return ((GameObject::_i_Entity*)(entity))->hierarchy.kids.front()+(index-1) ;
}

void EntityParent(TEntity entity, TEntity parent, int global)
{
	//Remove entity from its previous parent child list
	if ( GetParent ( entity ) )
		((GameObject::_i_Entity*)(GetParent(entity)))->hierarchy.kids.remove ( entity );

	((GameObject::_i_Entity*)(entity))->hierarchy.parent = parent;
	if ( parent )
		((GameObject::_i_Entity*)(parent))->hierarchy.kids.push_back ( entity );
}

TEntity GetParent(TEntity entity)
{
	return ((GameObject::_i_Entity*)(entity))->hierarchy.parent;
}

TEntity FindChild(TEntity entity, const_str name)
{
	for ( list<TEntity>::iterator it = ((GameObject::_i_Entity*)(entity))->hierarchy.kids.begin() ; it != ((GameObject::_i_Entity*)(entity))->hierarchy.kids.end() ; ++it )
	{
		if ( strcmp ( ((GameObject::_i_Entity*)(*it)) -> name.c_str() , name ) == 0 )
			return *it;
	}

	return NULL;
}

}